import express from "express";
import cors from "cors";
import { env } from "./config/env";
import { healthHandler } from "./routes/health";
import {
  getDemoMarket,
  getDemoOrders,
  getDemoPortfolio,
  postDemoOrder,
} from "./routes/demo";
import {
  getGameTime,
  setGameTimeSpeed,
  pauseGameTime,
  resumeGameTime,
  toggleGameTime,
  jumpToDate,
  skipToNextTradingDay,
} from "./routes/gameTime";
import {
  getAllMarketQuotes,
  getMarketQuote,
  getPriceHistory,
} from "./routes/market";

const app = express();

// 中间件
app.use(cors());
app.use(express.json());

// REST 路由
app.get("/health", healthHandler);

// Demo 交互接口（内存版本，仅用于原型演示）
app.get("/api/demo/portfolio", getDemoPortfolio);
app.get("/api/demo/market", getDemoMarket);
app.get("/api/demo/orders", getDemoOrders);
app.post("/api/demo/order", postDemoOrder);

// 游戏时间引擎接口
app.get("/api/game/time", getGameTime);
app.post("/api/game/time/speed", setGameTimeSpeed);
app.post("/api/game/time/pause", pauseGameTime);
app.post("/api/game/time/resume", resumeGameTime);
app.post("/api/game/time/toggle", toggleGameTime);
app.post("/api/game/time/jump", jumpToDate);
app.post("/api/game/time/skip-day", skipToNextTradingDay);

// 市场数据接口
app.get("/api/market/quotes", getAllMarketQuotes);
app.get("/api/market/quote/:symbol", getMarketQuote);
app.get("/api/market/history/:symbol", getPriceHistory);

// 预留 tRPC 接入点（后续会用 tRPC router 挂载到 /trpc）
// app.use("/trpc", trpcMiddleware);

app.listen(env.port, () => {
  // eslint-disable-next-line no-console
  console.log(
    `TraderTycoon backend listening on port ${env.port} in ${env.nodeEnv} mode`,
  );
});

